package com.czk.factory.support;

import com.czk.factory.config.BeanDefinition;
import com.czk.exception.BeansException;
import com.czk.factory.BeanFactory;

/**
 * @Author:ChenZhangKun
 * @Date: 2023/2/19 10:21
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
    @Override
    public Object getBean(String name) throws BeansException {
        // 从父类获取
        Object bean = super.getSingleton(name);
        if (bean!=null){
            return bean;
        }
        // 单例池中没有，再拿到class对象进行实例化
        BeanDefinition beanDefinition=getBeanDefinition(name);
        return createBean(name,beanDefinition);
    }

    /**
     * 创建对象
     *
     * @param name
     * @param beanDefinition
     * @return
     */
    protected abstract Object createBean(String name, BeanDefinition beanDefinition) throws BeansException;

    /**
     * 获取对象包装
     *
     * @param name
     * @return
     * @throws BeansException
     */
    protected abstract BeanDefinition getBeanDefinition(String name) throws BeansException;

}
